package com.sj.excel;

import java.util.List;

/**
 * 创建工具
 * @param:
 * @return:
 * @author zhangsj
 * @date: 2021/12/30 23:49
 */
public class CreateUtil {

    /*
     * @Author zhangsj
     *  属性的字符串拼接
     * @Date 13:05 2021/12/31
     * @Param [types, attributes, mark, oa, notNulls, classType]
     * @return java.lang.String
     **/
    public static String appendPrivate(List types, List attributes, List<String> mark, List oa, List notNulls, String classType) {
        StringBuffer sb = new StringBuffer();
        if (types.size() == attributes.size() && types.size() == mark.size()) {
            for (int i = 0; i < mark.size(); i++) {
                sb.append("    /**\n");
                sb.append(" *" + mark.get(i) + "\n");
                sb.append(" */\n");
                if (ExcelConst.CLASS_TYPE_IN.equals(classType)){
                    if("是".equals(notNulls.get(i))){
                        // 截取 --> 之前字符串
                        String substring = mark.get(i);
                        if (mark.get(i).indexOf("-->") != -1){
                            substring = mark.get(i).substring(0, mark.get(i).indexOf("-->"));
                        }
                        sb.append(" @NotBlank(message = \""+substring+"不能为空\")\n");
                    }
                }
                if ("Date".equals(types.get(i))){
                    sb.append(" @JsonFormat(pattern = \"yyyy-MM-dd\",timezone = \"GMT+8\")\n");
                }
//                sb.append(" @JSONField(name = \"" + oa.get(i) + "\")\n"); /*JSONField的注解*/
                sb.append(" private " + types.get(i) + " " + attributes.get(i) + ";\n\n");
            }

        }
        return sb.toString();
    }

    /*
     * @Author zhangsj
     *  拼接getSet方法
     * @Date 13:07 2021/12/31
     * @Param [types, attributes, mark]
     * @return java.lang.String
     **/
    public static String getSet(List types, List<String> attributes, List mark) {

        StringBuffer sb = new StringBuffer();
        if (types.size() == attributes.size()) {

            for (int i = 0; i < mark.size(); i++) {

                String attribute = attributes.get(i);
                String c = String.valueOf(attribute.charAt(0));
                c = c.toUpperCase();
                attribute = c + attribute.substring(1);
                sb.append("    /**\n");
                sb.append(" * 设置" + mark.get(i) + "\n");
                sb.append(" * @param " + attributes.get(i) + " " + mark.get(i) + "\n");
                sb.append(" */\n");
                sb.append(" public void set" + attribute + "(" + types.get(i) + " " + attributes.get(i) + "){\n");
                sb.append("    this." + attributes.get(i) + " = " + attributes.get(i) + ";\n");
                sb.append(" }\n\n");
                sb.append("    /**\n");
                sb.append(" *获得" + mark.get(i) + "\n");
                sb.append(" * @return " + attributes.get(i) + " " + mark.get(i) + "\n");
                sb.append(" */\n");
                sb.append(" public " + types.get(i) + " get" + attribute + "(){\n");
                sb.append("    return " + attributes.get(i) + ";\n");
                sb.append(" }\n\n");
            }

        }

        return sb.toString();
    }

//    .toLowerCase();//转成小写

//.toUpperCase();//转成大写

    /**
     *
     * @param attributes
     * @return
     */
    public static List<String> attributesFilter(List<String> attributes) {
        for (int i = 0; i < attributes.size(); i++) {
            String str[] = attributes.get(i).toLowerCase().split("_");
            String Str = "";
            Str += str[0];
            for (int j = 1; j < str.length; j++) {
                Str += str[j].substring(0, 1).toUpperCase();
                Str += str[j].substring(1);
            }
            attributes.set(i, Str);
        }
        return attributes;
    }

    /**
     * 类型匹配转换
     * @param types
     * @return
     */
    public static List typesFilter(List<String> types) {
        for (int i = 0; i < types.size(); i++) {

            if (types.get(i).toLowerCase().contains("int")) {

                types.set(i, "Integer");
            } else if (types.get(i).toLowerCase().contains("double")) {

                types.set(i, "Double");
            } else if (types.get(i).toLowerCase().contains("string")) {

                types.set(i, "String");
            } else if (types.get(i).toLowerCase().contains("float")) {

                types.set(i, "Float");
            } else if (types.get(i).toLowerCase().contains("bool")) {

                types.set(i, "boolean");
            } else if (types.get(i).toLowerCase().contains("date")) {

                types.set(i, "Date");
            } else if (types.get(i).toLowerCase().contains("varchar")) {

                types.set(i, "String");
            }

        }
        return types;
    }
}
